package cn.wtg.kotlinlib.net.downloadAndUpload.download

import android.content.Context
import android.util.Log
import cn.wtg.kotlinlib.net.base.ApiConfigConstant.READ_TIMEOUT
import cn.wtg.kotlinlib.net.base.ApiConfigConstant.WRITE_TIMEOUT
import cn.wtg.kotlinlib.net.base.SSL.initSSLSocketFactory
import cn.wtg.kotlinlib.net.base.SSL.initTrustManager
import okhttp3.logging.HttpLoggingInterceptor
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import okhttp3.*
import java.util.concurrent.TimeUnit

/**
 * author: wtg
 * date:2020/4/2 0002
 * desc: 需要监听下载进度的工具类
 */
object DownLoadUtils {
    /**
     * 下载文件 无断点续传
     *
     * @param url              地址
     * @param downloadListener 下载监听
     * @param callback         下载完成回调
     */
    fun download(
        context: Context?,
        url: String,
        downloadListener: DownloadResponseBody.DownloadListener?,
        callback: Callback?
    ): Call {
        val request: Request = Request.Builder()
            .url(url)
            .build()
        val httpLoggingInterceptor =
            HttpLoggingInterceptor { message: String -> Log.e("okhttp4_download:", message) }
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        val builder = OkHttpClient.Builder()
        builder.readTimeout(READ_TIMEOUT.toLong(), TimeUnit.SECONDS)
            .writeTimeout(WRITE_TIMEOUT.toLong(), TimeUnit.SECONDS)
            .sslSocketFactory(initSSLSocketFactory(), initTrustManager())
            .addInterceptor(httpLoggingInterceptor)
            .addInterceptor(DownloadInterceptor(downloadListener))
            .cookieJar(PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(context)))
        // 发起请求
        val call: Call = builder.build().newCall(request)
        call.enqueue(callback!!)
        return call
    }

    /**
     * 下载文件 断点续传
     *
     * @param url              地址
     * @param downloadListener 下载监听
     * @param startPoint       开始下载的位置
     * @param callback         下载完成回调
     */
    fun downloadBreakpointResume(
        context: Context?,
        url: String,
        downloadListener: DownloadResponseBody.DownloadListener?,
        startPoint: Long,
        callback: Callback?
    ): Call {
        val request: Request = Request.Builder()
            .url(url)
            .header("RANGE", "bytes=$startPoint-") //断点续传
            .build()
        val httpLoggingInterceptor =
            HttpLoggingInterceptor { message: String -> Log.e("okhttp4_download:", message) }
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        val builder = OkHttpClient.Builder()
        builder.readTimeout(READ_TIMEOUT.toLong(), TimeUnit.SECONDS)
            .writeTimeout(WRITE_TIMEOUT.toLong(), TimeUnit.SECONDS)
            .sslSocketFactory(initSSLSocketFactory(), initTrustManager())
            .addInterceptor(httpLoggingInterceptor)
            .addInterceptor(DownloadInterceptor(downloadListener, startPoint))
            .cookieJar(PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(context)))
        // 发起请求
        val call: Call = builder.build().newCall(request)
        call.enqueue(callback!!)
        return call
    }
}